package org.example.blog.dao;

import org.example.blog.pojo.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

public interface UserDao extends JpaRepository<User, String>, JpaSpecificationExecutor<User> {
    
    /**
     * 根据用户名查找用户
     * @param username
     * @return 
     */
    User findOneByUsername(String username);

    /**
     *  根据邮箱查找
     * @param email
     * @return
     */
    User findOneByEmail(String email);

    User findOneById(String UserId);

    /**
     *
     * 通过修改用户的状态来删除用户（假删除）
     *
     * @param userId
     * @return
     */
    @Modifying
    @Query(nativeQuery = true, value = "UPDATE `tb_user` SET `state` = '0' WHERE `id` = ?")
    int deleteUserByState(String userId);


    /**
     * 重写分页查询 避免分页查询带上密码
     * @param pageable
     * @return org.springframework.data.domain.Page<org.example.blog.pojo.User>
     */
    @Query(value = "select new User(u.id, u.username, u.roles, u.avatar, u.email, u.sign, u.state, u.regIp, u.loginIp, u.createTime, u.updateTime ) from User as u")
    Page<User> listAllUserNoPassword(Pageable pageable);
}
